package com.bfxy.rabbitmq.returnlistener;

import com.bfxy.rabbitmq.util.RabbitUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ReturnListener;

import java.io.IOException;

/**
 * Created by deng_wb on 2020/3/11
 *
 * 处理不可路由的消息
 */
public class Producer {

    public static void main(String[] args) throws Exception{
        Channel channel = RabbitUtil.producerChannel();

        String exchange = "test_return_exchange";
        String routingKey = "return.save";
        String routingKeyError = "abc.save";

        String msg = "Hello RabbitMQ Return Message";

        channel.addReturnListener((int replyCode, String replyText, String exchange_,
                                   String routingKey_, AMQP.BasicProperties properties, byte[] body) -> {
            System.err.println("---------handle  return----------");
            System.err.println("replyCode: " + replyCode);
            System.err.println("replyText: " + replyText);
            System.err.println("exchange: " + exchange_);
            System.err.println("routingKey: " + routingKey_);
            System.err.println("properties: " + properties);
            System.err.println("body: " + new String(body));

        });

        /** mandatory 如果设为 true,监听器会接收到路由不可达的消息,然后进行后续处理,如果为false,那么broker端会自动删除该消息  **/
        channel.basicPublish(exchange,routingKeyError,true,null,msg.getBytes());


    }

}
